package cn.bgs.serviceedu.controller;

import cn.bgs.serviceedu.entity.Customer;
import cn.bgs.serviceedu.entity.Doctor;
import cn.bgs.serviceedu.service.CustomerService;
import cn.bgs.serviceedu.service.DoctorService;
import cn.bgs.serviceutils.utils.JwtUtils;
import cn.bgs.serviceutils.utils.R;
import cn.bgs.serviceutils.utils.RedisUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author:YiChen
 * @program: IntelliJ IDEA
 * @Description:
 * @Created 2021/10/23-9:49
 */
@Api(description = "登录表")
@RestController
@RefreshScope
@RequestMapping("/serviceedu/login")
public class LoginController {


    @Autowired
    private CustomerService customerService;

    @Autowired
    private DoctorService doctorService;

    @Autowired
    private RedisUtils redisUtils;

    /**
     * 对象方法登录
     * @param customer
     * @return
     */
    @ApiOperation(value = "客户登录登录")
    @PostMapping("/CustomerLogin")
    @CachePut(key = "'CustomerToken'",value = "CustomerToken")
    public R login(@RequestBody Customer customer){
        return R.ok().data("token",customerService.login(customer));
    }


    /**
     * 对象方法登录
     * @param doctor
     * @return
     */
    @ApiOperation(value = "医生登录登录")
    @PostMapping("/DoctorLogin")
    @CachePut(key = "'DoctorToken'",value = "DoctorToken")
    public R login(@RequestBody Doctor doctor){
        return R.ok().data("token",doctorService.login(doctor));
    }


    /**
     * 根据token获取登录用户信息
     * @param request
     * @return
     */
    @ApiOperation(value = "根据token获取登录客户信息")
    @GetMapping("/getCustomerInfo")

    public R getCustomerInfo(HttpServletRequest request){
        Object  customerToken = redisUtils.get("CustomerToken");
        if (customerToken!=null){
            Integer memberIdByJwtToken = JwtUtils.getMemberIdByJwtToken((HttpServletRequest) customerToken);
            return R.ok().data("Customer",customerService.getById(memberIdByJwtToken));
        }

        //调用JWT方法，根据request获取用户信息
        Integer memberIdByJwtToken = JwtUtils.getMemberIdByJwtToken(request);
        //查询用户信息
        return R.ok().data("Customer",customerService.getById(memberIdByJwtToken));

    }


    /**
     * 根据token获取登录用户信息
     * @param request
     * @return
     */
    @ApiOperation(value = "根据token获取登录医生信息")
    @GetMapping("/getDoctorInfo")
    @CachePut(key = "'Doctor'",value = "Doctor")
    public R getDoctorInfo(HttpServletRequest request){
        //调用JWT方法，根据request获取用户信息
        Integer memberIdByJwtToken = JwtUtils.getMemberIdByJwtToken(request);
        //查询用户信息
        return R.ok().data("Doctor",doctorService.getById(memberIdByJwtToken));
    }


}
